﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using StockLib;
using StockLib.Data;

namespace StockUI
{
    public partial class FormBase : FormAbstractChild
    {
        public FormBase()
        {
            InitializeComponent();
        }

        protected override void ProcessNotifyMessage(FormNotifyMessage msg)
        {
            base.ProcessNotifyMessage(msg);

            switch (msg.Message)
            {
                case "Base.Show":
                    ShowBase(msg.Param1 as Stock);
                    break;
            }
        }

        private void ShowBase(Stock stock)
        {
            List<BaseData> baseData = StockData.Instance.LoadBaseData(stock);

            pnlColumn.PerformLayout();

            pnlColumn.Controls.Clear();
            txtContent.Clear();
            lblName.Text = stock.Name;
            lblCode.Text = stock.Code;

            foreach (BaseData data in baseData)
            {
                Button btn = new Button();
                btn.FlatStyle = FlatStyle.Flat;
                btn.FlatAppearance.CheckedBackColor = Color.Black;
                btn.FlatAppearance.MouseDownBackColor = Color.Black;
                btn.FlatAppearance.MouseOverBackColor = Color.Black;
                btn.FlatAppearance.BorderColor = Color.Aqua;
                btn.FlatAppearance.BorderSize = 1;
                btn.ForeColor = Color.White;
                btn.BackColor = Color.Black;
                btn.Text = data.Column;
                btn.Tag = data;
                btn.Margin = new Padding(2);
                btn.Click += new EventHandler(btnColumn_Click);
                pnlColumn.Controls.Add(btn);
            }
            pnlColumn.PerformLayout();

            if (pnlColumn.Controls.Count > 0)
            {
                btnColumn_Click(pnlColumn.Controls[0], new EventArgs());
            }
        }

        private void btnColumn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            foreach (Control c in pnlColumn.Controls)
            {
                c.ForeColor = (c == btn) ? Color.Red : Color.White;
            }
            txtContent.Focus();

            BaseData data = btn.Tag as BaseData;
            txtContent.Text = data.Content;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Back:
                    break;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            FindFormChild("Report");
        }
    }
}
